package com.fsz.pachong.pachong.util;


import com.fsz.pachong.pachong.entity.Record;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PachongUtil {
    private static Logger logger = LoggerFactory.getLogger(PachongUtil.class);
    private static CloseableHttpClient httpClient = HttpClients.custom().build();
    public static final String URL = "https://fuwu.most.gov.cn/html/jgcx/";
    public static final String BASE_URL = "https://fuwu.most.gov.cn";
    public static String keyWord1 = "华大";
    public static String keyWord2 = "鲲远";
    public static String keyWord3 = "山西省人民医院";
    public static String keyWord4 = "太原市第七人民医院";

    public static List<Record> pachong(String url, String charset) {
        List<Record> records = new ArrayList<>();
        String postUrl = url;
        logger.info("url地址{}:" + postUrl);
        // 第一页爬取
        HttpGet httpGet = HttpClientConnectionManager.getGetMethod(postUrl + "index.html");
        String m = null;
        try {
            CloseableHttpResponse response = httpClient.execute(httpGet);
            String html = InputStream2String(response.getEntity().getContent(), charset);
            List<Record> onePage = parseHtml(html, "1");
            records.addAll(onePage);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // 其他页面爬取
        for (int i = 2; i < 27; i++) {
            httpGet = HttpClientConnectionManager.getGetMethod(postUrl + "index_" + i + ".html");
            try {
                CloseableHttpResponse response = httpClient.execute(httpGet);
                String html = InputStream2String(response.getEntity().getContent(), charset);
                List<Record> otherPage = parseHtml(html, i + "");
                records.addAll(otherPage);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return records;
    }

    public static String pachongTianLong(String url) {
        String postUrl = url;
        HttpGet httpGet = HttpClientConnectionManager.getGetMethod(postUrl);
        try {
            CloseableHttpResponse response = httpClient.execute(httpGet);
            String html = InputStream2String(response.getEntity().getContent(), "GB2312");
            System.out.println("爬取的内容====》" + html);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return "";
    }

    public static String InputStream2String(InputStream in_st, String charset) throws IOException {
        BufferedReader buff = new BufferedReader(new InputStreamReader(in_st, charset));
        StringBuffer res = new StringBuffer();
        String line = "";
        while ((line = buff.readLine()) != null) {
            res.append(line);
        }
        in_st.close();
        return res.toString();
    }

    /**
     * @function: <p>
     * 解析页面Html
     * </p>
     */
    public static List<Record> parseHtml(String html, String page) {
        List<Record> records = new ArrayList<>();
        Document document = Jsoup.parse(html);
        // 获得信息列表标题列表
        Elements titleElements = document.getElementsByClass("right-min");
        if (titleElements == null || titleElements.size() == 0) {
            return null;
        } else {

            for (int i = 0; i < titleElements.size(); i++) {
                Element el = titleElements.get(i);
                Elements lis = el.getElementsByTag("li");
                // 获取标题
                for (int j = 0; j < lis.size(); j++) {
                    Element element = lis.get(j);
                    String href = element.child(0).attr("href");
                    HttpGet httpGet = HttpClientConnectionManager.getGetMethod(BASE_URL + href);
                    try {
                        CloseableHttpResponse response = httpClient.execute(httpGet);
                        String sub_html = InputStream2String(response.getEntity().getContent(), "utf-8");
                        Record record = parseSubHtml(sub_html, page);
                        if (record != null) {
                            records.add(record);
                        }
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                }
            }
            return records;
        }

    }

    public static Record parseSubHtml(String html, String page) {
        Document document = Jsoup.parse(html);
        Record record = new Record();
        record.setPage(page);
        Element title = document.getElementById("xq_t");
        if (title != null) {
            System.out.println("title===>" + title.text());
            record.setTitle(title.text());
            record.setKeyWord(getKeyWord(record.getTitle()));
        }
        Element date = document.getElementById("xq_t2");
        if (date != null) {
            System.out.println("date===>" + date.text());
            record.setDate(date.text());
        }
        Element text = document.getElementById("xp_zw");
        if (text != null) {
            System.out.println("text===>" + text.text());
            record.setText(text.text());
            record.setKeyWord(getKeyWord(record.getText()));
        }
    /*    if(StringUtils.isEmpty(record.getKeyWord())) {
            return null;
        }*/
        Elements imgUrl = document.getElementById("xp_zw").getElementsByTag("img");
        if (imgUrl != null && imgUrl.size() > 0) {
            StringBuffer img_url = new StringBuffer();
            for (int i = 0; i < imgUrl.size(); i++) {
                String imgUrlDo = imgUrl.get(i).attr("src");
                if (!imgUrlDo.contains("https") && !imgUrlDo.contains("http")) {
                    imgUrlDo = BASE_URL + imgUrlDo;
                }
                if (imgUrlDo.toLowerCase().contains("jpg")) {
                    img_url.append(imgUrlDo + ";");
                    String url = img_url.toString();
                    url = url.substring(0, url.length() - 1);
                    record.setImgUrl(url);
                    System.out.println("img地址==" + url);
                }
            }
        }
        Elements pdfUrl = document.getElementById("xp_zw").getElementsByTag("a");
        if (pdfUrl != null && pdfUrl.size() > 0) {
            StringBuffer pdf_url = new StringBuffer();
            for (int i = 0; i < pdfUrl.size(); i++) {
                String pdfUrlDo = pdfUrl.get(i).attr("href");
                if (!pdfUrlDo.contains("https") && !pdfUrlDo.contains("http")) {
                    pdfUrlDo = BASE_URL + pdfUrlDo;
                }
                if(pdfUrlDo.toLowerCase().contains("pdf") || pdfUrlDo.toLowerCase().contains("doc") ||  pdfUrlDo.toLowerCase().contains("docx")) {
                    pdf_url.append(pdfUrlDo + ";");
                    String url = pdf_url.toString();
                    url = url.substring(0, url.length() - 1);
                    record.setPdfUrl(url);
                    System.out.println("pdf地址==" + url);
                }
            }
        }
        return record;
    }

    private static String zhengzeUtils(String r) {
        Pattern p = Pattern.compile("\'(.*?)\'");
        Matcher m = p.matcher(r);
        int i = 0;
        m.find();
        return m.group().replace("'", "");
    }

    private static String zhengzeUtilsTwo(String r) {
        Pattern p = Pattern.compile("（(.*?)）");
        Matcher m = p.matcher(r);
        int i = 0;
        m.find();
        return m.group(1).replace("'", "");
    }

    public static String getKeyWord(String text) {
        if (text.contains(keyWord1)) {
            return keyWord1;
        }
        if (text.contains(keyWord2)) {
            return keyWord2;
        }
        if (text.contains(keyWord3)) {
            return keyWord3;
        }
        if (text.contains(keyWord4)) {
            return keyWord4;
        }
        return "";
    }

    public static void main(String[] args) {
//		String a=zhengzeUtilsTwo("动作片（香港）影片推荐");
//		System.out.println(a);
        pachong(URL, "UTF-8");

    }
}
